
package com.hlkj.pay.task;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.order.IReceiveOrderAppService;
import com.hlkj.pay.app.order.IWithdrawOrderAppService;
import com.hlkj.pay.app.payment.IMerchantPaymentAppService;
import com.hlkj.pay.dto.order.PayOrderQueryDto;
import com.hlkj.pay.dto.order.WithdrawOrderQueryDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.order.OrderWithdrawDO;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;
import com.hlkj.pay.infrastructure.model.payment.MerchantPaymentDetailDO;
import com.hlkj.pay.service.order.IPayOrderService;
import com.hlkj.pay.service.order.IWithdrawOrderService;
import com.hlkj.pay.service.payment.IMerchantPaymentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author HlpayTeam
 * @date 2024/10/29 10:43
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class OrderStatusTasks {

    private final IPayOrderService payOrderService;

    private final IWithdrawOrderService withdrawOrderService;

    private final IWithdrawOrderAppService withdrawOrderAppService;

    private final IMerchantPaymentService merchantPaymentService;

    private final IMerchantPaymentAppService merchantPaymentAppService;

    private final IReceiveOrderAppService receiveOrderAppService;

    @Value("${server.port:8020}")
    private String serverPort;

    @Value("${hlkj.task.port:8020}")
    private String taskPort;

    @Scheduled(cron = "0 * * * * ?")
    public void expireOrderPayJob() {
        if (!serverPort.equals(taskPort)) {
            log.info("当前 expireOrderPayJob port 非目标port serverPort:{},taskPort:{}", serverPort, taskPort);
            return;
        }
        // 你的任务逻辑
        PayOrderQueryDto payOrderQueryDto = new PayOrderQueryDto();
        payOrderQueryDto.setStates(new HashSet<>() {

            {
                add(PayOrderEnums.PAY_ORDER_STATUS.INIT.getCode());
                add(PayOrderEnums.PAY_ORDER_STATUS.PAYING.getCode());
            }
        });
        payOrderQueryDto.setPageNo(1);
        payOrderQueryDto.setPageSize(100);
        payOrderQueryDto.setMaxExpiredTime(System.currentTimeMillis());
        PageResult<PayOrderDO> payOrderDOPageResult = payOrderService.queryPage(payOrderQueryDto);
        List<PayOrderDO> list = payOrderDOPageResult.getList();
        if (CollectionUtils.isEmpty(list)) {
            log.info("当前 expireOrderPayJob 执行没有需要关闭的订单");
            return;
        }
        log.info("当前 expireOrderPayJob 执行 需要关闭的订单数量为 size:{}", list.size());
        list.stream().forEach(payOrderDO -> {
            payOrderService.closePayOrder(payOrderDO);
        });
    }

    @Scheduled(cron = "0 0 * * * ?")
    public void expireOrderWithdrawJob() {
        if (!serverPort.equals(taskPort)) {
            log.info("当前 expireOrderWithdrawJob port 非目标port serverPort:{},taskPort:{}", serverPort, taskPort);
            return;
        }
        // 你的任务逻辑
        WithdrawOrderQueryDto payOrderQueryDto = new WithdrawOrderQueryDto();
        payOrderQueryDto.setStatusList(new ArrayList<>() {

            {
                add(MerchantEnum.PROCESS_STATUS.INIT.getCode());
                add(MerchantEnum.PROCESS_STATUS.DOING.getCode());
            }
        });

        List<OrderWithdrawDO> list = withdrawOrderService.queryList(payOrderQueryDto);
        if (CollectionUtils.isEmpty(list)) {
            log.info("当前 expireOrderWithdrawJob 执行没有需要关闭的订单");
            return;
        }
        log.info("当前 expireOrderWithdrawJob 执行 需要关闭的订单数量为 size:{}", list.size());
        list.stream().forEach(payOrderDO -> {
            withdrawOrderAppService.queryPayStatus(payOrderDO);
        });
    }

    @Scheduled(cron = "0 30 * * * ?")
    public void expireOrderPaymentJob() {
        if (!serverPort.equals(taskPort)) {
            log.info("当前 expireOrderPaymentJob port 非目标port serverPort:{},taskPort:{}", serverPort, taskPort);
            return;
        }
        List<MerchantPaymentDetailDO> list = merchantPaymentService.queryDetailList(null, MerchantEnum.PROCESS_STATUS.DOING.getCode());
        if (CollectionUtils.isEmpty(list)) {
            log.info("当前 expireOrderPaymentJob 执行没有需要关闭的订单");
            return;
        }
        log.info("当前 expireOrderPaymentJob 执行 需要关闭的订单数量为 size:{}", list.size());
        list.stream().forEach(payOrderDO -> {
            log.info("当前 expireOrderPaymentJob 代付明细id :{}", payOrderDO.getDetailPaymentId());
            merchantPaymentAppService.syncDetailStatus(payOrderDO.getDetailPaymentId());
        });
    }


    @Scheduled(cron = "0 0 */1 * * ?")
    public void receiveOrderStatusJob() {
        if (!serverPort.equals(taskPort)) {
            log.info("当前 receiveOrderStatusJob port 非目标port serverPort:{},taskPort:{}", serverPort, taskPort);
            return;
        }
        receiveOrderAppService.syncStatus();
    }
}
